import Vue from 'vue'
import VueRouter from 'vue-router'

//引入公用函数storage
import storage from "@/utils/storage"

Vue.use(VueRouter)
    //配置路由

//引入组件
import Layout from "@/views/Layout.vue"
import Login from "@/views/Login.vue"

//动态路由

//改造路由文件，并暴露出去
export const asyncRoutes = [{
        path: '/',
        redirect: '/login',
        isshow: false,
    },
    //下面都是Layout下面的二级路由

    {
        path: '/login',
        component: Login,
        isshow: false,
    },
    //二级路由不配在Layout里，提高性能
    {
        path: '',
        component: Layout,
        isshow: true,
        icon: "iconfont icon-solid-home",
        children: [{
            path: '/home',
            meta: {
                title: '后台首页'
            },
            component: () =>
                import ('@/views/home/Home.vue') //路由懒加载
        }]
    },

    {
        path: '',
        component: Layout,
        isshow: true,
        icon: 'iconfont icon-dingdan',
        children: [{
            path: '/order',
            meta: {
                title: '订单管理'
            },
            component: () =>
                import ('@/views/order/Order.vue') //订单管理没有二级路由
        }]
    },
    {
        path: '/goods',
        component: Layout,
        isshow: true,
        icon: 'iconfont icon-goods-copy',
        meta: {
            title: '商品管理'
        },
        redirect: '/goods/goodsList',
        children: [{
                path: 'goodsList',
                meta: {
                    title: '商品列表'
                },
                component: () =>
                    import ('@/views/goods/GoodsList.vue')
            },
            {
                path: 'goodsAdd',
                meta: {
                    title: '商品添加'
                },
                component: () =>
                    import ('@/views/goods/GoodsAdd.vue')
            },
            {
                path: 'goodsSort',
                meta: {
                    title: '商品分类'
                },
                component: () =>
                    import ('@/views/goods/GoodsSort.vue')
            },
        ]
    },
    {
        path: '',
        component: Layout,
        isshow: true,
        icon: 'iconfont icon-jinrudianpu',
        children: [{
            path: '/store',
            meta: {
                title: '店铺管理'
            },
            component: () =>
                import ('@/views/store/Store.vue')
        }]
    },



    {
        path: '/account',
        component: Layout,
        isshow: true,
        icon: 'iconfont icon-zhanghao',
        meta: {
            title: '账号管理',
            roles: ['super', 'normal']
        },
        redirect: '/account/accountAdd', //重定向一下
        children: [{
                path: 'accountAdd',
                meta: {
                    title: '账号添加',
                    roles: ['super']
                },
                component: () =>
                    import ('@/views/account/AccountAdd.vue')
            },
            {
                path: 'accountList',
                meta: {
                    title: '账号列表',
                    roles: ['super']
                },
                component: () =>
                    import ('@/views/account/AccountList.vue')
            },
            {
                path: 'changePwd',
                meta: {
                    title: '修改密码',
                    roles: ['super', 'normal']
                },
                component: () =>
                    import ('@/views/account/ChangePwd.vue')
            },
        ]
    },
    {
        path: '/count',
        component: Layout,
        isshow: true,
        icon: 'iconfont icon-tongji',
        meta: {
            title: '销售统计',
            roles: ['super']
        },
        redirect: '/count/goodsCount',
        children: [{
                path: 'goodsCount', //这样写:一级路由的hash和二级路由的hash拼接在一起
                meta: {
                    title: '商品统计',
                    roles: ['super']
                },
                component: () =>
                    import ('@/views/count/GoodsCount.vue')
            },
            {
                path: 'orderCount',
                meta: {
                    title: '订单统计',
                    roles: ['super']
                },
                component: () =>
                    import ('@/views/count/OrderCount.vue')

            }
        ]
    },
    {
        path: '',
        component: Layout,
        isshow: false,
        children: [{
            path: '/personal',
            meta: {
                title: '个人中心'
            },
            component: () =>
                import ('@/views/personal/Personal.vue') //路由懒加载
        }]
    },

    //错误页面-->没有配置hash值时，访问这个页面
    {
        path: '*',
        component: () =>
            import ('@/views/Erro.vue'),
        isshow: false,
    }
]



const router = new VueRouter({
    routes: asyncRoutes
})

//前置路由守卫-->写在路由配置里面，在hash值变化时会执行
// 路由守卫写在路由暴露之前 这个方法会传递一个回调函数 会传递3个参数
//from-->从哪里来   to--->到哪里去  next--->放行函数
router.beforeEach((to, from, next) => {
    //登录页面只要访问就放行
    if (to.path == '/login') {
        next();
    } else if (storage.getSession("tk")) {
        //如果tk存在,放行所有
        next();
    } else if (storage.getSession("tk")) {
        //如果会话存储里的tk被删了，跳转到login页面
        router.push('/login');
    }

})


//解决跳转时相同路由报错
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}

export default router